package cn.qiao.mdc.cglib;

import cn.qiao.mdc.util.Util;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class MDCInterceptor implements MethodInterceptor {

    private final Object target;

    public MDCInterceptor(Object target) {
        this.target = target;
    }

    @Override
    public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        return Util.trace(() -> target != null ? method.invoke(target, args) : methodProxy.invokeSuper(proxy, args));
    }
}
